﻿/*
 * Autore: Artur Tolstenco
 */

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using RecipesManager.Data;

namespace RecipesManager.MainControls.RecipeSubControls
{
    /// <summary>
    /// Interaction logic for IngredientSubControl.xaml
    /// </summary>
    public partial class IngredientSubControl : UserControl
    {
        IngredientFromRecipe _currentIngredient;
        String _currentRecipeName;
        IEnumerable<string> _existingIngredients;

        MeasureFamily _currentMeasureFamily;


        public string CurrentRecipeName
        {
            get { return _currentRecipeName; }
            set { _currentRecipeName = value; }
        }

        public IEnumerable<string> ExistingIngredients
        {
            get { return _existingIngredients; }
            set { _existingIngredients = value; }
        }

        public IngredientFromRecipe CurrentIngredient
        {
            get { return _currentIngredient; }
            set { 
                _currentIngredient = value;
                MyInitialize();
            }
        }

        public MeasureFamily CurrentMeasureFamily
        {
            get { return _currentMeasureFamily; }
            set { _currentMeasureFamily = value; }
        }

        public IngredientSubControl()
        {
            InitializeComponent();
        }

        void MyInitialize()
        {
            //popolazione
            cb_ingredients.Items.Clear();
            cb_ingredients.Items.Add(Application.Current.FindResource("ComboBoxAddNewIngredientSuggestion"));
            foreach (var i in Data.Operations.Operations.GetAddableIngredientsForRecipe(_currentRecipeName, _existingIngredients))
                cb_ingredients.Items.Add(i.Name);

            /* nuovo ingrediente */
            if (_currentIngredient == null)
            {
                tb_title.Text = Application.Current.FindResource("AddIngredientToRecipeTitle") + " " + _currentRecipeName;
                bt_salva.Content = new Image { Source = new BitmapImage(new Uri("../../Icons/document-add.png", UriKind.Relative)) };
                bt_salva.ToolTip = Application.Current.FindResource("AddIngredientToRecipeTooltip");

                cb_ingredients.IsEnabled = true;
                tb_notes.Text = "";
                tb_quantity.Text = "";
                UpdateQuantityCombobox();


            }
            else
            {
                tb_title.Text = Application.Current.FindResource("EditIngredientFromRecipeTitle") + " " + _currentRecipeName;
                bt_salva.Content = new Image { Source = new BitmapImage(new Uri("../../Icons/document-save.png", UriKind.Relative)) };
                bt_salva.ToolTip = Application.Current.FindResource("SaveButtonTooltip");

                cb_ingredients.Items.Add(_currentIngredient.Name);
                cb_ingredients.SelectedItem = _currentIngredient.Name;
                cb_ingredients.IsEnabled = false;

                UpdateQuantityCombobox(_currentIngredient.Name);
                cb_measures.SelectedItem = _currentIngredient.Measure;

                tb_notes.Text = _currentIngredient.Notes;
                tb_quantity.Text = _currentIngredient.Quantity.ToString(CultureInfo.InvariantCulture);

            }

            bt_salva.IsEnabled = IsModified();
        }

        void UpdateQuantityCombobox(string ingredientName = "")
        {
            if (ingredientName.Equals(""))
            {
                cb_measures.Items.Clear();
                return;
            }

            cb_measures.Items.Clear();
            var currentMeasureType = Manager.Ingredients[ingredientName].MeasureType;
            var currentMeasures = Data.Operations.Operations.GetOrderedMeasures(_currentMeasureFamily, currentMeasureType);
            foreach (var m in currentMeasures)
                cb_measures.Items.Add(m.Name);
        }

        private void Cb_Ingredients_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var comboBox = sender as ComboBox;
            if (comboBox == null) return;

            var selectedIngredient = cb_ingredients.SelectedItem as string;
            if (selectedIngredient == null)
                return;
            if (selectedIngredient.Equals(Application.Current.FindResource("ComboBoxAddNewIngredientSuggestion")))
            {
                var res = (new IngredientSubControls.IngredientAddWindow()).ShowDialog();
                if (res != null && res == true)
                {
                    selectedIngredient = Manager.Ingredients.LastAdded.Name;
                    cb_ingredients.Items.Insert(1, selectedIngredient);
                    cb_ingredients.SelectedIndex = 1;
                    UpdateQuantityCombobox(selectedIngredient);
                    tb_quantity.Focus();
                }
                else
                {
                    cb_ingredients.SelectedItem = null;
                }
            } 
            else
            {
                UpdateQuantityCombobox(selectedIngredient);
                tb_quantity.Focus();
            }

            bt_salva.IsEnabled = IsModified();
        }

        public bool IsModified()
        {
            if (_currentIngredient == null)
                if (cb_ingredients.SelectedIndex != -1 || cb_measures.SelectedIndex != -1 ||
                 !tb_quantity.Text.Equals("") || !tb_notes.Text.Equals(""))
                    return true;
                else
                    return false;

            var selectedMeasure = cb_measures.SelectedItem as string;
            if (selectedMeasure == null)
                return true;

            return !selectedMeasure.Equals(_currentIngredient.Measure) ||
                   !tb_quantity.Text.Equals(_currentIngredient.Quantity.ToString(CultureInfo.InvariantCulture)) ||
                   !tb_notes.Text.Equals(_currentIngredient.Notes);
        }



        private void Tb_Notes_TextChanged(object sender, TextChangedEventArgs e)
        {
            bt_salva.IsEnabled = IsModified();
        }

        private void Cb_Measures_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            bt_salva.IsEnabled = IsModified();
            tb_notes.Focus();
        }
    }
}
